import matplotlib.pyplot as plt
import numpy as np

# 数据
normalization_methods = ['None', 'BatchNorm (BN)', 'LayerNorm (LN)', 'GroupNorm (GN)']
mnist_accuracy = [98.0, 98.2, 98.0, 98.3]
cifar10_accuracy = [48.1, 60.9, 60.3, 60.6]

# 设置柱状图的宽度和位置
bar_width = 0.35
x = np.arange(len(normalization_methods))

# 定义柔和的颜色
mnist_color = '#FFB74D'  # 柔和的橙色
cifar10_color = '#A191E0'  # 柔和的紫色

# 创建分组柱状图
fig, ax = plt.subplots(figsize=(10, 6))

bars1 = ax.bar(x - bar_width/2, mnist_accuracy, bar_width, label='MNIST', color=mnist_color)
bars2 = ax.bar(x + bar_width/2, cifar10_accuracy, bar_width, label='CIFAR-10', color=cifar10_color)

# 添加标签和标题
ax.set_xlabel('Normalization Methods', fontsize=14)
ax.set_ylabel('Accuracy (%)', fontsize=14)
ax.set_title('Model Performance on MNIST and CIFAR-10 Datasets', fontsize=16)
ax.set_xticks(x)
ax.set_xticklabels(normalization_methods, fontsize=12)
ax.legend(fontsize=12)

# 调整 y 轴范围和网格
plt.ylim(0, 100)
ax.yaxis.grid(color='grey', linestyle='--', linewidth=0.7, alpha=0.5)

# 让每个条形的顶部显示它的值
def add_value_labels(bars):
    for bar in bars:
        yval = bar.get_height()
        ax.text(bar.get_x() + bar.get_width() / 2, yval, round(yval, 1), ha='center', va='bottom')

add_value_labels(bars1)
add_value_labels(bars2)

# 调整布局并保存图形
plt.tight_layout()
plt.savefig('normalization_performance.png', bbox_inches='tight', dpi=300)  # 保存为 PNG 文件